import time

import requests
from bs4 import BeautifulSoup
import random
import json

from config import *


def get_response(url):
    response = requests.get(url=url,
                            headers=random.choice(headers),
                            proxies=random.choice(proxy))
    with open('pink_floyd.html', 'w', encoding='utf-8') as file:
        file.write(response.text)


def get_album_discography():
    list_album = []
    with open('pink_floyd.html', 'r', encoding='utf-8') as file:
        html = file.read()

    soup = BeautifulSoup(html, 'lxml')
    table_body = soup.find('tbody').find_all('tr')
    for content in table_body:
        content = content.find('td', class_='title').find('a')
        name = content.text
        href = content.get('href')
        list_album.append({'name': name, 'link': href})
    dict_album = list_album
    with open('albums_Pink_floyd.json', 'w', encoding='utf-8') as file:
        json.dump(dict_album, file, indent=4)


def get_tracks_in_album():
    with open('albums_Pink_floyd.json', 'r', encoding='utf-8') as file:
        albums = json.load(file)

    for album in albums:
        list_content = []
        response = requests.get(url=f'{album["link"]}',
                                headers=random.choice(headers),
                                proxies=random.choice(proxy))
        print(album['name'])
        time.sleep(4)
        soup = BeautifulSoup(response.text, 'lxml')

        trs_tbody = soup.find_all('tbody')

        for trs_tbodys in trs_tbody:
            trs_tbodys = trs_tbodys.find_all('tr')

            for tr in trs_tbodys:
                title_composer = tr.find('td', class_='title-composer')
                title = title_composer.find('div', class_='title').text.strip()
                composer = title_composer.find('div', class_='composer').text.strip()
                time_track = tr.find('td', class_='time').text.strip()
                list_content.append(
                    {
                        'title': title,
                        'composer': composer,
                        'time': time_track
                    }
                )

            with open(f'Tracks_album/tracks of album - {album["name"]}.json', 'a', encoding='utf-8') as file:
                json.dump(list_content, file, indent=4)


if __name__ == '__main__':
    get_tracks_in_album()
